package com.doublefish.doublefishrobot.ui.home.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.doublefish.doublefishrobot.R;
import com.doublefish.doublefishrobot.ui.home.bean.ModeSettingBean;

/**
 * 作者:dugaofeng on 2024/10/18 16:54
 */
public class ModeSettingItemLoopContentView extends LinearLayout implements ModeSettingItemView.OnModeSettingItemViewListner {

    private ModeSettingBean.ModeSettingLoopBean settingLoopBean;

    private ModeSettingItemView loopItemView;       //训练循环次数 时长
    private ModeSettingItemView intervalItemView;   //训练循环间隔

    private OnModeSettingItemLoopContentViewListner onModeSettingItemLoopContentViewListner;


    public ModeSettingItemLoopContentView(@NonNull Context context) {
        this(context, null);
    }

    public ModeSettingItemLoopContentView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ModeSettingItemLoopContentView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
        initListner();
    }

    public void setOnModeSettingItemLoopContentViewListner(OnModeSettingItemLoopContentViewListner onModeSettingItemLoopContentViewListner) {
        this.onModeSettingItemLoopContentViewListner = onModeSettingItemLoopContentViewListner;
    }

    private void initView() {
        inflate(getContext(), R.layout.view_mode_loop_content, this);

        loopItemView = findViewById(R.id.item_loop);
        intervalItemView = findViewById(R.id.item_interval);
    }

    private void initListner() {
        loopItemView.setOnModeSettingItemViewListner(this);
        intervalItemView.setOnModeSettingItemViewListner(this);
    }

    public void setSettingLoopBean(ModeSettingBean.ModeSettingLoopBean settingLoopBean) {
        this.settingLoopBean = settingLoopBean;

        //0.训练循环次数  1.训练时长
        if (settingLoopBean.getLoopType() == 0) {
            loopItemView.setMutiItem(true);
            loopItemView.setItemTag(ModeSettingBean.ModeSettingItemTag.ModeSettingItemTagLoopCount);
            loopItemView.setSettingItemBean(settingLoopBean.getLoopCountItemBean());
        } else {
            loopItemView.setMutiItem(true);
            loopItemView.setItemTag(ModeSettingBean.ModeSettingItemTag.ModeSettingItemTagLoopTime);
            loopItemView.setSettingItemBean(settingLoopBean.getLoopTimeItemBean());
        }

        intervalItemView.setItemTag(ModeSettingBean.ModeSettingItemTag.ModeSettingItemTagLoopInterval);
        intervalItemView.setSettingItemBean(settingLoopBean.getLoopIntervalItemBean());   //循环间隔
    }

    @Override
    public void onModeSettingItemViewValueClick(ModeSettingItemView itemView) {
        if (onModeSettingItemLoopContentViewListner != null) {
            onModeSettingItemLoopContentViewListner.onModeSettingItemValueClick(settingLoopBean, itemView.getSettingItemBean(), itemView.getItemTag());
        }
    }

    public interface OnModeSettingItemLoopContentViewListner {
        void onModeSettingItemValueClick(ModeSettingBean.ModeSettingLoopBean loopBean, ModeSettingBean.ModeSettingItemBean itemBean, ModeSettingBean.ModeSettingItemTag itemTag);
    }
}
